﻿using System;
using System.Threading;

namespace Tools
{
    /// <summary>
    /// 函数防抖
    /// </summary>
    public class Debounce
    {
        private readonly TimeSpan delay;
        private Timer timer;

        public Debounce(TimeSpan delay)
        {
            this.delay = delay;
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="action"></param>
        public void Execute(Action action)
        {
            timer?.Dispose();
            timer = new Timer(_ => action(), null, delay, Timeout.InfiniteTimeSpan);
        }
    }
}